var InitURL = getInitUrls();
//搜索预加载
// $(document).ready(function(){
    // $.ajax({
        // type: 'POST',
        // url: '/index.php?m=Ajaxsearch&a=def',
        // data: {id: 1},
        // dataType: 'json',
        // success: function (rs) {
            // $('#history-header').html('');
            // $('#xiala-header').html('');
            // var html = '';
            // var html2 = '';
            // var num = 1;
            // for (var i in rs.new) {
                // if(i >7){
                    // break;
                // }

                // if(rs.new[i] != null){
                    // html += '<a href="javascript:;" onclick="on_search_header('+ "'"+ rs.new[i].name + "'" +',this)">'+ rs.new[i].name + '</a>';
                    // $('#history-header').html(html);
                    // num++;
                // }
            // }
            // var l = 1;
            // for (var j in rs.newTop) {
                // if (l <= 3) {
                    // html2 += '<li class="z_spanName_header">';
                    // html2 +=    '<a id="u_header'+num+'" onclick="on_search_header('+ "'"+rs.newTop[j].name + "'" +',this)" href="javascript:;" class="every-record">';
                    // html2 +=       '<span class="record-name">' + rs.newTop[j].name + '</span>';
                    // html2 +=       '<span class="record-number-wrap"><span class="record-number">' + rs.newTop[j].count + '</span>结果</span>';
                    // html2 +=    '</a>';
                    // html2 +='</li>';
                    // num++;
                // } else {
                    // html2 += '<li class="z_spanName_header">';
                    // html2 +=    '<a id="u_header'+num+'" onclick="on_search_header('+ "'"+rs.newTop[j].name + "'" +',this)" href="javascript:;" class="every-record">';
                    // html2 +=       '<span class="record-name">' + rs.newTop[j].name + '</span>';
                    // html2 +=       '<span class="record-number-wrap"><span class="record-number">' + rs.newTop[j].count + '</span>结果</span>';
                    // html2 +=    '</a>';
                    // html2 +='</li>';
                // }
                // num++;
                // l++
                // $('#xiala-header').html(html2);
            // }
        // }
    // });
// });

/**
 * 下拉点击搜索
 */
function on_search_header(keyword,e,isHistory){
    var isHistory = isHistory ? true : false;
    var isRecommend = isHistory ? false : true;
    //搜索数据埋点
    $.getJSON('http://api.ibaotu.com/?m=search&a=getSearchResultCount&callback=?',{k:keyword},function(res){
        var resultCount = Number(res);
        sa.track('search', {
            keyWord: keyword,
            resultCount: resultCount,
            isHistory: isHistory,
            isRecommend: isRecommend,
            searchPosition: '头部悬浮搜索框'
        });
    });

    $('.home .search-tips-bannner').hide();
    $('#head-classify-header').val(keyword)
    var sort =  $(e).attr('id')
    $('#sort').val(sort)
    search_header(keyword)
}


/**
 * 搜索JS
 */
var formSearch_header = function(){
    var kw = $('#head-classify-header').val();
    if(kw == '' || kw == '搜索素材'){
        alert('请填写关键词');
        return false;
    }
	location.href='/plus/search.php?q='+kw;
    //搜索数据埋点
	/*
    $.getJSON('http://api.ibaotu.com/?m=search&a=getSearchResultCount&callback=?',{k:kw},function(res){
        var resultCount = Number(res);
        sa.track('search', {
            keyWord: kw,
            resultCount: resultCount,
            isHistory: false,
            isRecommend: false,
            searchPosition: '头部悬浮搜索框'
        });
    });
    search_header(kw);
    $.getJSON(InitURL.AJAX+"/?m=clickstats&lx=500&callback=?", function(e) {});
	*/
}


$('.head-search-wrap').on('click', formSearch_header);


$('#head-classify-header').keydown(function(event){if(event.which == 13){formSearch_header();}});


function search_header(kw){
    $.get("/index.php?m=search&a=check", {kw:kw}, function(data) {
        if(data.status == 1){
            tj_keyword()
            setTimeout(function(){
                if(data.rewrite){
                    window.location.href="/index.php?m=tags&a=index&class=0-0-0-0-0-1&key=" + data.py;
                }else{
                    window.location.href=InitURL.DOMAIN+"/tupian/"+data.py+".html";
                }
            },500);
        }else{
            alert('请填写有效的关键词');
        }
    }, 'json');
}



/**
 * 搜索
 */
$("#head-classify-header").keyup(function(H){
    if (window.event) {
        var G = window.event.keyCode;
    } else {
        var G = H.which;
    }
    if (G != 38 && G != 40 && G != 13) {
        var num = 1;
        D = 0;
        if($('#head-classify-header').val() == ''){
            //默认搜索词
        }else{
            $('#sort').remove()
            $.ajax({
                type: 'POST',
                url: '/index.php?m=Ajaxsearch&a=index',
                data: {keyword: $('#head-classify-header').val()},
                dataType: 'json',
                success: function (rs) {
                    $('#history-header').html('');
                    $('#xiala-header').html('');
                    var html = '';
                    for (var i in rs) {
                        html += '<li class="z_spanName_header">';
                        html +=    '<a id="u_header'+num+'" onclick="on_search('+ "'"+rs[i].keyword + "'" +',this)" href="javascript:;" class="every-record">';
                        html +=       '<span class="record-name">' + rs[i].keyword + '</span>';
                        html +=       '<span class="record-number-wrap"><span class="record-number">' + rs[i].icount + '</span>结果</span>';
                        html +=    '</a>';
                        html += '</li>';
                        $('#xiala-header').html(html);
                        num++
                    }

                    if(num <= 1){
                        $('.float-search-tips').hide();
                    }else{
                        $('.float-search-tips').show();
                    }
                }
            });
        }
    } else {
        topkey_header(H)
    }
});

/**
 * 下键选中
 */
var D = 0;
function topkey_header(H) {
    if (window.event) {
        var G = window.event.keyCode;
    } else {
        var G = H.which;
    }
    if (G != 38 && G != 40 && G != 13) {
        D = 0;
    } else {
        if ($(".header-search-tips").css("display") == "block") {
            var I = $(".z_spanName_header").length;
            if (G == 38) {
                D--;
                if (D < 1) {
                    D = I;
                    C_header(D);
                    F_header(1);
                } else {
                    F_header(D + 1);
                    C_header(D);
                }
                return false;
            }
            if (G == 40) {
                D++;
                if (D > I) {
                    D = 1;
                    C_header(1);
                    F_header(I);
                } else {
                    F_header(D - 1);
                    C_header(D);
                }
                return false;
            }
        } else {
            D = 0;
        }
    }
}
function C_header(G) {
    $("#u_header" + G).parent().css("background-color", "#EBEBEB");
    var name = $("#u_header" + G + " .record-name").html()
    $('#head-classify-header').val(name)
}
function F_header(G) {
    $("#u_header" + G).parent().css("background-color", "#fff");
}

function search_header(kw){
    $.get(InitURL.DOMAIN+"/index.php?m=search&a=check", {kw:kw}, function(data) {
        if(data.status == 1){
            tj_keyword()
            setTimeout(function(){
                if(data.rewrite){
                    window.location.href=InitURL.DOMAIN+"/index.php?m=tags&a=index&class=0-0-0-0-0-1&key=" + data.py;
                }else{
                    window.location.href=InitURL.DOMAIN+"/tupian/"+data.py+".html";
                }
            },500);
        }else{
            alert('请填写有效的关键词');
        }
    }, 'json');
}